package controller

import (
	"github.com/gin-gonic/gin"
	"k8s-dash/dto"
	"k8s-dash/middleware"
	"k8s-dash/service"
	"k8s-dash/utils"
	"k8s-dash/utils/logger"
)

type serviceController struct{}

var ServiceController serviceController

func (p *serviceController) Register(ginRouter *gin.RouterGroup) {
	nd := &serviceController{}
	ginRouter.POST("/k8s/service/create", nd.CreateSVC)
	ginRouter.GET("/k8s/service/list", nd.GetSVCList)
	ginRouter.GET("/k8s/service/detail", nd.GetSVCDetail)
	ginRouter.DELETE("/k8s/service/delete", nd.DeleteSVC)
	ginRouter.PATCH("/k8s/service/update", nd.UpdateSVC)
	ginRouter.GET("/k8s/service/numnp", nd.GetSVCNP)
}

// CreateSVC 创建service
// ListPage godoc
// @Summary      创建service
// @Description  创建service
// @Tags         service
// @ID           /api/k8s/service/create
// @Accept       json
// @Produce      json
// @Param        body  body dto.ServiceCreateInput  true  "body"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": ""}"
// @Router       /api/k8s/service/create [post]
func (d *serviceController) CreateSVC(ctx *gin.Context) {
	params := &dto.ServiceCreateInput{}
	if err := params.BindValidate(ctx); err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
	}
	err := service.Service.CreateSVC(params)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.CreateError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.CreateError, err))
	}
	middleware.ResponseSuccess(ctx, "创建成功")
}

// GetSVCList 获取service列表，支持分页过滤排序
// ListPage godoc
// @Summary      获取service列表
// @Description  获取service列表
// @Tags         service
// @ID           /api/k8s/service/list
// @Accept       json
// @Produce      json
// @Param        filter_name  query  string  false  "过滤"
// @Param        namespace    query  string  false  "命名空间"
// @Param        page         query  int     false  "页码"
// @Param        limit        query  int     false  "分页限制"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": service.SvcResp}"
// @Router       /api/k8s/service/list [get]
func (d *serviceController) GetSVCList(ctx *gin.Context) {
	parmas := &dto.SvcListInput{}
	//参数验证
	err := parmas.BindValidate(ctx)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
		return
	}
	svcList, err := service.Service.GetSVCList(parmas.FilterName, parmas.Namespace, parmas.Limit, parmas.Page)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.GetError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.GetError, err))
		return
	}
	middleware.ResponseSuccess(ctx, svcList)
}

// GetSVCDetail 获取service详情
// ListPage godoc
// @Summary      获取service详情
// @Description  获取service详情
// @Tags         service
// @ID           /api/k8s/service/detail
// @Accept       json
// @Produce      json
// @Param        name  query  string  true  "name"
// @Param        namespace    query  string  true  "命名空间"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": coreV1.service}"
// @Router       /api/k8s/service/detail [get]
func (d *serviceController) GetSVCDetail(ctx *gin.Context) {
	parmas := &dto.SvcNameInput{}
	if err := parmas.BindValidate(ctx); err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
	}
	svcDetail, err := service.Service.GetSVCDetail(parmas.Namespace, parmas.Name)
	if err != nil {
		return
	}
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.GetError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.GetError, err))
		return
	}
	middleware.ResponseSuccess(ctx, svcDetail)
}

// DeleteSVC 删除service
// ListPage godoc
// @Summary      删除service
// @Description  删除service
// @Tags         service
// @ID           /api/k8s/service/delete
// @Accept       json
// @Produce      json
// @Param        name  query  string  true  "name"
// @Param        namespace    query  string  true  "命名空间"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": nil}"
// @Router       /api/k8s/service/delete [delete]
func (d *serviceController) DeleteSVC(ctx *gin.Context) {
	parmas := &dto.SvcNameInput{}
	if err := parmas.BindValidate(ctx); err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
	}
	err := service.Service.DeleteSVC(parmas.Namespace, parmas.Name)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.DeleteError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.DeleteError, err))
		return
	}
	middleware.ResponseSuccess(ctx, nil)
}

// UpdateSVC 更新service
// ListPage godoc
// @Summary      更新service
// @Description  更新service
// @Tags         service
// @ID           /api/k8s/service/update
// @Accept       json
// @Produce      json
// @Param        content  query  string  true  "更新内容"
// @Param        namespace    query  string  true  "命名空间"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": nil}"
// @Router       /api/k8s/service/update [patch]
func (d *serviceController) UpdateSVC(ctx *gin.Context) {
	parmas := &dto.SvcUpdateInput{}
	if err := parmas.BindValidate(ctx); err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
	}
	err := service.Service.UpdateSVC(parmas.Content, parmas.Namespace)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.UpdateError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.UpdateError, err))
		return
	}
	middleware.ResponseSuccess(ctx, nil)
}

// GetSVCNP 获取ns下service数量
// ListPage godoc
// @Summary      获取ns下service数量
// @Description  获取ns下service数量
// @Tags         service
// @ID           /api/k8s/service/deployNp
// @Accept       json
// @Produce      json
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": string}"
// @Router       /api/k8s/service/numnp [get]
func (p *serviceController) GetSVCNP(ctx *gin.Context) {
	stsToNS, err := service.Service.GetSVCNP()
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.GetError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.GetError, err))
		return
	}
	middleware.ResponseSuccess(ctx, stsToNS)
}
